package com.ayu.shortlink.admin.mq.consumer;

import com.ayu.shortlink.admin.common.constant.RocketMQConstant;
import com.ayu.shortlink.admin.dao.entity.CouponTaskDO;
import com.ayu.shortlink.admin.mq.basics.base.MessageWrapper;
import com.ayu.shortlink.admin.mq.basics.event.CouponTaskDelayExecuteEvent;
import com.ayu.shortlink.admin.service.CouponTaskService;
import lombok.RequiredArgsConstructor;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * 改变优惠券任务执行状态的消费者
 */
@Component
@RequiredArgsConstructor
@RocketMQMessageListener(
        topic = RocketMQConstant.TOPIC_EXECUTOR_DELAY_TASK,
        consumerGroup = RocketMQConstant.GROUP_DELAY_EXECUTOR_TASK_STATUS_CHANGE
)
public class CouponTaskDelayExecuteStatusConsumer implements RocketMQListener<MessageWrapper<CouponTaskDelayExecuteEvent>> {
    private final CouponTaskService couponTaskService;
    @Override
    public void onMessage(MessageWrapper<CouponTaskDelayExecuteEvent> messageWrapper) {
        // 修改延时执行推送任务任务状态为执行中
        CouponTaskDelayExecuteEvent message = messageWrapper.getMessage();
        CouponTaskDO couponTaskDO = CouponTaskDO.builder()
                .id(message.getCouponTaskId())
                .status(message.getStatus())
                .build();
        couponTaskService.updateById(couponTaskDO);
    }
}
